package com.jyw.business.ins.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyw.business.ast.domain.AstInfo;
import com.jyw.business.ast.domain.vo.web.InsUnitAstPageVO;
import com.jyw.business.ast.service.IAstInfoService;
import com.jyw.business.ins.domain.InsUnit;
import com.jyw.business.ins.domain.dto.web.InsUnitAddDto;
import com.jyw.business.ins.domain.dto.web.InsUnitEditDto;
import com.jyw.business.ins.domain.dto.web.InsUnitPageDto;
import com.jyw.business.ins.domain.dto.web.InsUnitStateDto;
import com.jyw.business.ins.domain.vo.web.InsUnitInfoVO;
import com.jyw.business.ins.domain.vo.web.InsUnitPageVO;
import com.jyw.business.ins.mapper.InsUnitMapper;
import com.jyw.business.ins.service.IInsUnitService;
import com.jyw.common.utils.DateUtils;
import com.jyw.system.utils.ConversionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 巡检单元Service业务层处理
 *
 * @author zhangcheng
 * @date 2023-09-21
 */
@Service
public class InsUnitServiceImpl extends ServiceImpl<InsUnitMapper, InsUnit> implements IInsUnitService {
    @Resource
    private InsUnitMapper insUnitMapper;

    @Resource
    private IAstInfoService iAstInfoService;

    /**
     * 查询巡检单元
     *
     * @param id 巡检单元主键
     * @return 巡检单元
     */
    @Override
    public InsUnit selectInsUnitById(Long id) {
        return insUnitMapper.selectInsUnitById(id);
    }

    @Override
    public List<InsUnitAstPageVO> findAstById(Long id) {
        AstInfo astInfo = new AstInfo();
        astInfo.setPertainUnit(id);
        List<AstInfo> astInfos = iAstInfoService.selectAstInfoList(astInfo);
        if (CollectionUtil.isNotEmpty(astInfos)) {
            List<InsUnitAstPageVO> pages=new ArrayList<>();
            astInfos.forEach(a->{
                InsUnitAstPageVO vo=new InsUnitAstPageVO();
                BeanUtils.copyProperties(a, vo);
                pages.add(vo);
            });
            return ConversionUtil.conversionList(pages, InsUnitAstPageVO.class);
        }
        return null;
    }

    @Override
    public InsUnitInfoVO findInfoById(Long id) {
        InsUnit insUnit = insUnitMapper.selectInsUnitById(id);
        if (insUnit != null) {
            InsUnitInfoVO insUnitInfoVO = new InsUnitInfoVO();
            BeanUtils.copyProperties(insUnit, insUnitInfoVO);
            AstInfo astInfo = new AstInfo();
            astInfo.setPertainUnit(id);
            List<AstInfo> astInfos = iAstInfoService.selectAstInfoList(astInfo);
            if (CollectionUtil.isNotEmpty(astInfos)) {
                insUnitInfoVO.setAstInfoList(astInfos);
            }
            return insUnitInfoVO;
        }
        return null;
    }

    /**
     * 查询巡检单元列表
     *
     * @param insUnit 巡检单元
     * @return 巡检单元
     */
    @Override
    public List<InsUnit> selectInsUnitList(InsUnit insUnit) {
        return insUnitMapper.selectInsUnitList(insUnit);
    }

    @Override
    public List<InsUnit> selectPlanUnitList() {
        return insUnitMapper.selectPlanUnitList();
    }

    @Override
    public List<InsUnitPageVO> pageInsUnitList(InsUnitPageDto insUnitPageDto) {
        List<InsUnitPageVO> list = insUnitMapper.pageInsUnitList(insUnitPageDto);
        return ConversionUtil.conversionList(list, InsUnitPageVO.class);
    }

    /**
     * 新增巡检单元
     *
     * @param insUnitAddDto 巡检单元
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertInsUnit(InsUnitAddDto insUnitAddDto) {
        InsUnit insUnit = new InsUnit();
        BeanUtils.copyProperties(insUnitAddDto, insUnit);
        insUnit.setCreateTime(DateUtils.getNowDate());
        insUnitMapper.insertInsUnit(insUnit);
        Long unitId = insUnit.getId();
        if (CollectionUtil.isNotEmpty(insUnitAddDto.getAstIdList())) {
            List<AstInfo> astInfos = new ArrayList<>();
            insUnitAddDto.getAstIdList().forEach(astId -> {
                AstInfo astInfo = new AstInfo();
                astInfo.setId(astId);
                astInfo.setPertainUnit(unitId);
                astInfos.add(astInfo);
            });
            iAstInfoService.saveOrUpdateBatch(astInfos);
        }
        return 1;
    }

    /**
     * 修改巡检单元
     *
     * @param insUnitEditDto 巡检单元
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateInsUnit(InsUnitEditDto insUnitEditDto) {
        InsUnit insUnit = new InsUnit();
        BeanUtils.copyProperties(insUnitEditDto, insUnit);
        Long unitId = insUnit.getId();
        AstInfo astInfo = new AstInfo();
        astInfo.setPertainUnit(unitId);
        List<AstInfo> oldAstInfos = iAstInfoService.selectAstInfoList(astInfo);
        if (CollectionUtil.isNotEmpty(oldAstInfos)) {
            List<AstInfo> oldAstInfos1 = oldAstInfos.stream().peek(a -> a.setPertainUnit(null)).collect(Collectors.toList());
            iAstInfoService.saveOrUpdateBatch(oldAstInfos1);
        }
        if (CollectionUtil.isNotEmpty(insUnitEditDto.getAstIdList())) {
            List<AstInfo> astInfos = new ArrayList<>();
            insUnitEditDto.getAstIdList().forEach(astId -> {
                AstInfo astInfo1 = new AstInfo();
                astInfo1.setId(astId);
                astInfo1.setPertainUnit(unitId);
                astInfos.add(astInfo1);
            });
            iAstInfoService.saveOrUpdateBatch(astInfos);
        }
        return insUnitMapper.updateInsUnit(insUnit);
    }

    @Override
    public int updateState(InsUnitStateDto insUnitStateDto) {
        InsUnit insUnit = new InsUnit();
        insUnit.setId(insUnitStateDto.getId());
        insUnit.setIsEnable(insUnitStateDto.getIsEnable());
        return insUnitMapper.updateState(insUnit);
    }

    /**
     * 批量删除巡检单元
     *
     * @param ids 需要删除的巡检单元主键
     * @return 结果
     */
    @Override
    public int deleteInsUnitByIds(Long[] ids) {
        return insUnitMapper.deleteInsUnitByIds(ids);
    }

    /**
     * 删除巡检单元信息
     *
     * @param id 巡检单元主键
     * @return 结果
     */
    @Override
    public int deleteInsUnitById(Long id) {
        return insUnitMapper.deleteInsUnitById(id);
    }
}
